/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rhinestone.TUI;
import rhinestone.Control.StaffController;
import rhinestone.Model.Staff;

/**
 *
 * @author Group 2
 */
public class StaffUI extends UI {
	private static final String RANK_MESSAGE = "\t|0| - Cashier\n\t|1| - Manager\nRank: ";
	private static final String WORKING_PLACE_MESSAGE = "\t|0| - DIY\n\t|1| - Timberyard\nWorking place: ";
    private StaffController controller;

    public StaffUI(StaffController controller) {
        this.controller = controller;
    }

    public void startMenu()
    {
        boolean finished = false;
        while(!finished){
			printMenu();
			int choise = promptChoiceInput();
			switch(choise){
				case 1:
					addStaff();
					break;
				case 2:
					printStaffInformation();
					break;
				case 3:
					editStaffInformation();
					break;
				case 4:
					deleteEmployee();
					break;
				case 5:
					finished = true;
					break;
			   default:
					break;
			}

        }
    }
	protected void printMenu() {
		System.out.println(" ----- CRUD PRODUCT MENU -----");
		System.out.println("\t(-1-) Add Employee");
		System.out.println("\t(-2-) Print Emplyee's Information");
		System.out.println("\t(-3-) Edit Employee's Information");
		System.out.println("\t(-4-) Delete Employee");
		System.out.println("\t(-5-) Back");
	}

    private void addStaff(){
		String name = promptStringInput(NAME_MESSAGE);
		String address = promptStringInput(ADDRESS_MESSAGE);
		String cpr = promptStringInput(CPR_MESSAGE);
		String phone = promptStringInput(PHONE_MESSAGE);
		int workingPlace = promptIntInput(WORKING_PLACE_MESSAGE);
		int rank = promptIntInput(RANK_MESSAGE);
		controller.addStaff(name, address, cpr, phone, workingPlace, rank);
    }
    private void printStaffInformation(){
		String cpr = promptStringInput(CPR_MESSAGE);
		Staff staff = controller.getStaff(cpr);
		if(staff != null)
			printStaff(staff);
		else
			System.err.println("Staff with CPR " + cpr + " does NOT exists.");
    }
    private void editStaffInformation(){
		String cpr = promptStringInput(CPR_MESSAGE);
		Staff staff = controller.removeStaff(cpr);
		if(staff != null) {
			System.out.println(" --- EDIT STAFF ---");
			System.out.println("Leave field blank to keep the existing value.");
			System.out.println("Old name: " + staff.getName());
			String name = promptStringInput(NAME_MESSAGE);
			System.out.println("Old address: " + staff.getAddress());
			String address = promptStringInput(ADDRESS_MESSAGE);
			System.out.println("Old CPR: " + staff.getCpr());
			cpr = promptStringInput(CPR_MESSAGE);
			System.out.println("Old phone nr: " + staff.getPhone());
			String phone = promptStringInput(PHONE_MESSAGE);
			System.out.println("Old working place: " + staff.getWorkingPlace());
			String workingPlace = promptStringInput(WORKING_PLACE_MESSAGE);
			System.out.println("Old rank: " + staff.getRank());
			String rank = promptStringInput(RANK_MESSAGE);

			controller.updateStaff(staff, name, address, cpr, phone, workingPlace, rank);
		}
		else
			System.err.println("Employee with CPR " + cpr + " does NOT exists.");
    }
    private void deleteEmployee(){
		String cpr = promptStringInput(CPR_MESSAGE);
		Staff staff = controller.removeStaff(cpr);
		if(staff != null)
			System.out.println(" --- Employee removed ---");
		else
			System.err.println("Employee with CPR " + cpr + " NOT found.");
    }

	private void printStaff(Staff staff) {
		System.out.println(staff);
	}

	/**
	 * Validates the login.
	 *
	 * @param cpr CPR of the employee
	 * @param psw The password.
	 * @return The staff if login is successful. Null otherwise.
	 */
	@Override
	public Staff validateEmployee(String cpr, String psw) {
		Staff staff = controller.getStaff(cpr);
		String last4cprDigits = cpr.substring(cpr.length() - 4);
		if(staff != null && last4cprDigits.equals(psw))
			return staff;
		return null;
	}
}
